//************************************************
//  *Author：jxx
//  *QQ：283591387
//  *自定义业务逻辑扩展
//************************************************
import { checkOpenGPSServiceByAndroid } from '../../util/getGPS.js';

export default function () {
	return {
		data() {
			return {
				off: true
			};
		},
		methods: {
			onInited() {
				this.fabButtons.push([
					{
						icon: 'error',
						value: '自定义',
						hidden: false,
						onClick: () => {}
					}
				]);
				console.log('this.searchFormFields', this.$store.state.userInfo.userName);
				if (this.$store.state.userInfo.userName) {
					this.searchFormFields.Creator = this.$store.state.userInfo.userName;
				}

				console.log('权限---', this.filterPermission('q_client_visit', 'ALL'));
			},
			filterPermission(tableName, permission) {
				console.log('this.$store.state.permission', this.$store.state.permission);
				const _result = (this.$store.state.permission || []).find((x) => {
					return x.tableName == tableName;
				});
				console.log('权限---_result', _result);
				return _result && _result.permission.some((x) => x == permission);
			},
			LocationONOFF() {
				let systemType = uni.getSystemInfoSync().platform;
				if (systemType == 'android') {
					let context = plus.android.importClass('android.content.Context');
					let locationManager = plus.android.importClass('android.location.LocationManager');
					let main = plus.android.runtimeMainActivity();
					let mainSvr = main.getSystemService(context.LOCATION_SERVICE);
					let off = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
					if (!off) {
						uni.showModal({
							title: '提示',
							content: '请打开定位服务功能',
							showCancel: false,
							success() {
								if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
									let Intent = plus.android.importClass('android.content.Intent');
									let Settings = plus.android.importClass('android.provider.Settings');
									let intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									main.startActivity(intent); //打开系统设置定位服务功能页面
								} else {
									console.log('定位服务功能已开启');
								}
							}
						});
					}
				}
			},
			getLocationFn() {
				uni.getLocation({
					type: 'wgs84', // 返回可以用于uni.openLocation的经纬度
					success: (res) => {
						// console.log('当前位置的经度：' + res.longitude);
						// console.log('当前位置的纬度：' + res.latitude);
						this.editFormFields.Longitude = res.longitude;
						this.editFormFields.Latitude = res.latitude;

						return {
							Longitude: res.longitude,
							Latitude: res.latitude
						};
					},
					fail: (error) => {
						console.error('获取位置失败：', error);
						uni.showToast({
							icon: 'none',
							title: '获取位置失败'
						});
						this.LocationONOFF();
					}
				});
			},
			getGPS() {
				console.log(123123);
				// uni.getLocation({
				// 	type: 'wgs84',
				// 	geocode: true, //设置该参数为true可直接获取经纬度及城市信息
				// 	success: (res) => {
				// 		console.log('getGPS---', res);
				// 		this.editFormFields.Longitude = res.longitude;
				// 		this.editFormFields.Latitude = res.latitude;
				// 		// 获取当前位置
				// 		plus.geolocation.getCurrentPosition(
				// 			function (position) {
				// 				console.log('定位成功', position);
				// 				this.editFormFields.latitude = position.coords.latitude;
				// 				this.editFormFields.Longitude = position.coords.longitude;
				// 			},
				// 			function (error) {
				// 				console.log('定位失败', error);
				// 				checkOpenGPSServiceByAndroid();
				// 			},
				// 			{
				// 				enableHighAccuracy: true, // 是否使用高精度定位
				// 				maximumAge: 0, // 定位结果缓存时间
				// 				provider: 'amap' // 使用高德地图定位
				// 			}
				// 		);
				// 		if (this.off) {
				// 			this.gridSave();
				// 		}
				// 	},
				// 	fail: (ress) => {
				// 		//失败调用
				// 		console.log('失败啦--fail');
				// 		checkOpenGPSServiceByAndroid();
				// 	}
				// });
			},
			formatter(row, column, index) {
				//自定义格式化
				// if(column.field=='xx'){
				//  return '<a style="color:red;">' + row[column.field] + '</a>';
				// }
				//return row[column.field]
			},
			rowClick(index, row, column) {
				//行点击事件(默认触发编辑)
				return true;
			},
			cellClick(index, row, column) {
				//单元格击事件(默认触发编辑)
				//仅onInited中设置：this.direction = "horizontal", this.columns的字段设置为click=true后生效
				return true;
			},
			rowButtons(index, row) {
			},
			rowButtonClick(btn, index, row) {
				//列表显示的按钮点击事件
				//    if(btn.text=='删除'){
				// 	this.$toast('删除')
				// }
			},
			searchBefore(params) {
				//查询前
				return true;
			},
			updateBefore(formData) {
				//更新保存前操作
				return this.testAdd(formData);
			},
			addBefore(formData) {
				console.log('触发addBefore');
				//新建保存前操作
				this.off = true;
				// if (!formData.mainData.Longitude || !formData.mainData.Latitude) {
				// 	 this.getGPS();
				// 	return false;
				// }
				return this.testAdd(formData);
			},
			searchFormOnChange(field, value) {
				//查询弹出框下拉框或日期选中事件
				//if(field=="字段"){
				// 	console.log(value)
				//}
			},
			editFormOnChange(field, value) {
				//新建编辑弹出框下拉框或日期选中事件
				if (field == 'Client') {
					try {
						let url = `api/q_client_f/GetClientInfo?bm=` + value;
						this.http
							.post(url, {})
							.then(([res]) => {
								this.editFormFields.Client_flag = res.Client_flag;
								this.editFormFields.Client_man = res.Client_man;
								this.editFormFields.Client_tel = res.Client_tel;
								this.editFormFields.Position = res.Position;
							})
							.catch(() => {
								uni.showToast({
									icon: 'none',
									title: '获取客户信息失败'
								});
							});
					} catch (e) {
						console.log('获取客户信息失败');
					}
				}
			},
			modelOpenAfter() {
				//弹出框打开的方法
				// this.currentAction=='Add'//判断是新建还是编辑操作
				// this.LocationONOFF();
				this.off = false;
				console.log('this.currentAction', this.currentAction);
				let readOnlyList = this.editFormOptions.filter((item) => item.field == 'Longitude' || item.field == 'Latitude');
				readOnlyList.forEach((item) => (item.readonly = true));
				if (this.currentAction == 'Add') {
					// this.getLocationFn();
					// this.getGPS();
				} else {
				}
				return true;
			},
			testAdd(formData) {
				console.log('formData', formData);
				if (!formData.mainData.Visit_num) {
					uni.showToast({
						icon: 'none',
						title: '请输入拜访次数'
					});
					return false;
				}
				return true;
			}
		}
	};
}
